<template>
  <view>
    <BaseNavbar :title="title" />

    <BaseSearch placeholder="请输入商品名称" @search="search" />

    <ComList :loading-type="loadingType" :bottom="isShowChecked ? 128 : 0">
      <GoodsListCard
        v-for="(item, index) in listData"
        :key="index"
        :isShowCheck="isShowChecked"
        :info="item"
        @selectItem="selectItem(item)"
      />
    </ComList>
    <FixedAddIcon
      v-if="isFromHome"
      @onAdd="goGoodsAdd"
      @onImport="goGoodsImport"
      :isShowImport="true"
    />
    <view
      v-if="isFromParent"
      class="fixed-import flexRowBetween"
      :style="{ paddingBottom: 20 + vIphoneXBottomHeight + 'rpx' }"
    >
      <BaseCheck
        :checked.sync="allChecked"
        :title="'全选(' + selectNum + ')'"
        @changeCheck="changeCheckAll"
      />
      <BaseButton width="330" type="primary" @onClick="importGoods"
        >导 入</BaseButton
      >
    </view>
  </view>
</template>

<script>
import BaseNavbar from "@/components/base/BaseNavbar.vue";
import BaseSearch from "@/components/base/BaseSearch.vue";
import ComList from "@/components/list/ComList.vue";
import myPull from "@/mixins/myPull.js";
import GoodsListCard from "../components/cards/GoodsListCard.vue";
import BaseCheck from "@/components/base/BaseCheck.vue";
import BaseButton from "@/components/base/BaseButton.vue";
import FixedAddIcon from "@/components/common/FixedAddIcon.vue";
export default {
  components: {
    BaseNavbar,
    BaseSearch,
    ComList,
    GoodsListCard,
    BaseCheck,
    BaseButton,
    FixedAddIcon,
  },
  data() {
    return {
      title: "我的商品",
      fromData: "",
      allChecked: false,
      isShowChecked: false,
      goodsName: "",
      isParentGoods: false,
      isFromPlace: false, //点位来的
      isFromParent: false, //上级商品来的
      isFromHome: false, //首页来的
      isFromDeviceSelect: false, //绑定选择商品来的
      selectInfoList: [],
      selectNum: 0,
      hotel_id: "",
    };
  },
  methods: {
    getList(page, done) {
      let data = {
          goods_name: this.goodsName,
          page,
        },
        rtnData = null;
      if (this.isParentGoods) {
        rtnData = this.$u.api.getMyParentProducts(data);
      } else if (this.isFromPlace) {
        rtnData = this.$u.api.getHotelProducts({
          ...data,
          hotel_id: this.hotel_id,
        });
      } else {
        rtnData = this.$u.api.getMyProducts(data);
      }
      if (rtnData) {
        rtnData.then((res) => {
          let dataList = res.data;
          if (rtnData.length > 0 && this.isParentGoods) {
            //添加check属性
            rtnData.forEach((item) => (item.isCheck = false));
          }
          done(dataList);
        });
      }
    },
    goGoodsAdd() {
      uni.navigateTo({ url: "/pagesB/goods/GoodsAdd" });
    },
    goGoodsImport() {
      uni.navigateTo({ url: `/pagesB/goods/GoodsList?from=goodsImport` });
    },
    search(val) {
      this.goodsName = val;
      this.refresh();
    },
    selectItem(item) {
      if (typeof item.isCheck == "undefined") {
        this.$set(item, "isCheck", true);
      } else {
        item.isCheck = !item.isCheck;
      }
      //这里是选择商品后直接返回上局数据到上一页
      if (this.isFromDeviceSelect) {
        let { goods_name, market_price, original_img, goods_id } = item;
        let goodsInfo = {
          goods_name,
          good_price: market_price,
          original_img,
          default_stock: 1,
          goods_id,
        };
        let pages = getCurrentPages();
        let currPage = pages[pages.length - 1]; //当前页面
        let prevPage = pages[pages.length - 2]; //上一个页面
        //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
        prevPage.setData({
          goodsInfo,
          isGetGoodsInfo: true,
        });
        uni.navigateBack();
        return;
      }

      //检查选中数量
      this.selectInfoList = this.listData.filter((item) => item.isCheck);
      if (this.selectInfoList.length == this.listData.length) {
        this.allChecked = true;
      } else {
        this.allChecked = false;
      }
      this.selectNum = this.selectInfoList.length;
    },
    changeCheckAll() {
      this.allChecked = !this.allChecked;
      this.listData.forEach((item) => (item.isCheck = this.allChecked));
      if (this.allChecked) {
        this.selectNum = this.listData.length;
        this.selectInfoList = this.listData;
      } else {
        this.selectNum = 0;
        this.selectInfoList = [];
      }
    },
    importGoods() {
      if (this.selectNum <= 0) {
        return;
      }
      let addGoodsId = this.selectInfoList.map((item) => item.goods_id);
      console.log(addGoodsId);
      let data = {
        goods_ids: addGoodsId,
      };
      this.$u.api.addParentProducts(data).then((res) => {
        this.isShowSuccess("导入成功", 1, () => {}, true);
      });
    },
  },
  onLoad(opt) {
    this.fromData = opt?.from;
    if (this.fromData == "goodsImport") {
      //导入上级商品
      this.title = "上级商品";
      this.isShowChecked = true;
      this.isParentGoods = true;
      this.isFromParent = true;
    } else if (this.fromData == "place") {
      //点位来的
      this.hotel_id = opt.id;
      this.title = this.vPointName + "商品";
      this.isFromPlace = true;
    } else if (this.fromData == "deviceSelectGoods") {
      this.title = "我的商品";
      this.isFromDeviceSelect = true;
      this.isShowChecked = true;
    } else {
      this.isFromHome = true;
    }
    this.refresh();
  },
  onShow(e) {
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1]; // 当前页
    if (currPage.data.isDoRefresh == true) {
      // 是否刷新
      currPage.data.isDoRefresh = false;
      this.goodsName = "";
      this.refresh();
    }
  },
  mixins: [myPull()],
};
</script>
<style lang="scss">
page {
  background-color: $pageBgColor;
}
</style>
<style lang="scss" scoped>
.fixed-import {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  padding: 20rpx 30rpx;
  box-sizing: border-box;
  background-color: $uni-bg-color;
  z-index: 999999;
}
</style>
